package service

import (
	"core/coreentity"
	"core/coreqto"
	"core/coreservice"
	"core/corestruct"
	"core/myerrors"
)

type mainService struct {
}

// Page Action分页
func (p *mainService) Page(mm *coreqto.BasePageDtQto, ctx *corestruct.HttpContext) (*corestruct.Page[coreentity.HtCls], error) {
	var db = ctx.DB
	tx := db.Model(&coreentity.HtCls{})
	if mm.Id > 0 {
		tx = tx.Where("id=?", mm.Id)
	} else {
		var dtb, _ = mm.GetDtb()
		if dtb != nil {
			tx = tx.Where("edt_dt>=?", dtb)
		}

		var dte, _ = mm.GetDte()
		if dte != nil {
			tx = tx.Where("edt_dt<?", dte)
		}

		if "" != mm.Key {
			var keyValue = "%" + mm.Key + "%"
			tx = tx.Where("name like ? or full_name like ?", keyValue, keyValue)
		}
	}
	var svr = &coreservice.PageService[coreentity.HtCls]{}
	r, err := svr.Page(&mm.BasePageQto, ctx, tx)
	if err != nil {
		return nil, myerrors.NewByErr(err)
	}

	return r, nil
}

var (
	MainService = &mainService{}
)
